<?php

use think\migration\Migrator;
use think\migration\db\Column;

class UpdateCouponConfigProductId extends Migrator
{


    public function up()
    {


        $this->table('coupon_config')
            ->addColumn('old_quote_id', 'integer', ['limit' => 15, 'default' => 0, 'comment' => '旧的quote_id'])
            ->update();



        \app\model\CouponConfig::query()
            ->where('quote_id', '<>', 0)
            ->select()->each(function (\app\model\CouponConfig $couponConfig) {
                $couponProductChannel = \app\model\CouponProductChannel::query()
                    ->where('id', $couponConfig->quote_id)
                    ->find();
                if (!$couponProductChannel) {
                    return;
                }

                $couponConfig->old_quote_id = $couponConfig->quote_id;
                /**
                 * @var \app\model\CouponProductChannel $couponProductChannel
                 */
                $couponConfig->quote_id = $couponProductChannel->product_id;
                $couponConfig->save();
            });
    }

    public function down()
    {
        $this->table('coupon_config')
            ->removeColumn('old_quote_id')
            ->update();
    }


}
